package com.ss.android.article.base.feature.subscribe.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.RecyclerListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.frameworks.baselib.network.http.util.TaskInfo;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.app.c.ImageManager;
import com.ss.android.article.base.feature.main.IRecentFragment;
import com.ss.android.article.base.feature.main.IFeedManager;
import com.ss.android.article.base.feature.pgc.PgcActivity;
import com.ss.android.article.base.feature.subscribe.b.SubscribeManager;
import com.ss.android.article.base.feature.subscribe.b.SubscribeManager.SubscribeDataChangeListener;
import com.ss.android.article.base.feature.subscribe.model.EntryItem;
import com.ss.android.article.base.feature.subscribe.model.SubscribeData;
import com.ss.android.article.base.feature.subscribe.model.b;
import com.ss.android.article.base.feature.subscribe.model.d;
import com.ss.android.article.base.ui.TagView;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.AbsFragment;
import com.ss.android.common.app.LifeCycleMonitor;
import com.ss.android.common.callback.CallbackCenter;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.image.AvatarLoader;
import com.ss.android.j.ClickableBackgroundCompat;
import com.ss.android.messagebus.Subscriber;
import com.ss.android.messagebus.SubscriptionRouter;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.newmedia.a.TimeFormatter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public final class e extends AbsFragment implements IRecentFragment, SubscribeDataChangeListener {
    static final class e$a extends BaseAdapter implements RecyclerListener, LifeCycleMonitor {
        final class e$a$a {
            public View a;
            public ImageView b;
            public TextView c;
            public TextView d;
            public TextView e;
            public ImageView f;
            TagView g;
            public com.ss.android.article.base.feature.subscribe.model.d h;
            public Boolean i;

            e$a$a(byte arg1) {
                this();
            }

            private e$a$a() {

            }
        }

        private List a;
        private ImageManager b;
        private AvatarLoader c;
        private TaskInfo d;
        private TimeFormatter e;
        private Resources f;

        public e$a(Context arg9) {

            this.a = new ArrayList();
            this.b = new ImageManager(arg9);
            this.d = new TaskInfo();
            int v6 = arg9.getResources().getDimensionPixelSize(R.dimen.i6);
            int v4 = arg9.getResources().getDimensionPixelSize(R.dimen.i7);
            this.c = new AvatarLoader(com.ss.android.article.browser.R.drawable.m4,
                    this.d, this.b, v4,
                    new b(v4, v6, arg9.getResources().getDimensionPixelSize(R.dimen.i8)));
            this.e = new TimeFormatter(arg9);
            this.f = arg9.getResources();
        }

        public final void a(Collection arg2) {
            this.a.clear();
            this.a.addAll(arg2);
            this.notifyDataSetChanged();
        }

        static void a(e$a$a arg3) {
            if (arg3 != null && arg3.h != null) {
                com.ss.android.article.base.feature.subscribe.model.d v0 = arg3.h;
                if (v0.badge <= 0 && !v0.f) {
                    arg3.g.setVisibility(View.INVISIBLE);
                    return;
                }

                arg3.g.setVisibility(View.VISIBLE);
                if (v0.f) {
                    arg3.g.setDrawText("NEW");
                    return;
                }

                arg3.g.setNumber(v0.badge);
            }
        }

        public final com.ss.android.article.base.feature.subscribe.model.d a(int arg2) {
            return (com.ss.android.article.base.feature.subscribe.model.d) this.a.get(arg2);
        }

        public final void onDestroy() {
            if (this.c != null) {
                this.c.onDestroy();
            }
        }

        public final void onResume() {
            if (this.c != null) {
                this.c.onPause();
            }
        }

        public final void onPause() {
        }

        public final int getCount() {
            return this.a.size();
        }

        public final Object getItem(int arg2) {
            return this.a(arg2);
        }

        public final long getItemId(int arg3) {
            return ((long) arg3);
        }

        public final View getView(int arg12, View arg13, ViewGroup arg14) {
            int v10 = R.color.entry_subscribe_list_item_desc;
            if (arg13 == null) {
                arg13 = LayoutInflater.from(arg14.getContext()).inflate(com.ss.android.article.browser.R.layout.subscribe_entry_item, arg14, false);
                e$a$a v1 = new e$a$a((byte) 0);
                v1.a = arg13.findViewById(com.ss.android.article.browser.R.id.aba);
                v1.b = arg13.findViewById(com.ss.android.article.browser.R.id.icon);
                v1.c = arg13.findViewById(com.ss.android.article.browser.R.id.entry_name);
                v1.d = arg13.findViewById(com.ss.android.article.browser.R.id.description);
                v1.e = arg13.findViewById(com.ss.android.article.browser.R.id.last_time);
                v1.f = arg13.findViewById(com.ss.android.article.browser.R.id.divider);
                v1.g = arg13.findViewById(com.ss.android.article.browser.R.id.badge);
                arg13.setTag(v1);
            }

            Object v0 = this.a.get(arg12);
            EntryItem v3 = ((com.ss.android.article.base.feature.subscribe.model.d) v0).a;
            Object v1_1 = arg13.getTag();
            ((e$a$a) v1_1).h = ((com.ss.android.article.base.feature.subscribe.model.d) v0);
            this.c.loadAvatar(((e$a$a) v1_1).b, v3.mIconUrl);
            ((e$a$a) v1_1).c.setText(v3.mName);
            ((e$a$a) v1_1).d.setText(((com.ss.android.article.base.feature.subscribe.model.d) v0).itemDescription);
            if (((com.ss.android.article.base.feature.subscribe.model.d) v0).lastTime > 0) {
                ((e$a$a) v1_1).e.setText(this.e.a(((com.ss.android.article.base.feature.subscribe.model.d) v0).lastTime * 1000));
            } else {
                ((e$a$a) v1_1).e.setText("");
            }

            ImageView v3_1 = ((e$a$a) v1_1).f;
            int v0_1 = arg12 == this.getCount() - 1 ? View.INVISIBLE : View.VISIBLE;
            v3_1.setVisibility(v0_1);
            e$a.a(((e$a$a) v1_1));
            if (v1_1 != null) {
                AppData.getInstance();
                boolean v2 = AppData.isNightMode();
                if (((e$a$a) v1_1).i != null && ((e$a$a) v1_1).i.booleanValue() == v2) {
                    return arg13;
                }

                ((e$a$a) v1_1).i = Boolean.valueOf(v2);
                ColorFilter v0_2 = v2 ? TTUtils.getNightModeColorFilter() : null;
                ((e$a$a) v1_1).b.setColorFilter(v0_2);
                ((e$a$a) v1_1).c.setTextColor(this.f.getColor(com.ss.android.article.browser.R.color.entry_subscribe_list_item_name));
                ((e$a$a) v1_1).d.setTextColor(this.f.getColor(v10));
                ((e$a$a) v1_1).e.setTextColor(this.f.getColor(v10));
                ((e$a$a) v1_1).f.setBackgroundColor(this.f.getColor(com.ss.android.article.browser.R.color.divider));
                ((e$a$a) v1_1).g.a();
                ClickableBackgroundCompat.setClickableBackground(((e$a$a) v1_1).a, v2);
            }

            return arg13;
        }

        public final void onStop() {
            if (this.c != null) {
                this.c.onStop();
            }
        }

        public final void onMovedToScrapHeap(View arg4) {
            Object v1 = null;
            if (arg4 != null) {
                Object v0 = arg4.getTag();
                if (!(v0 instanceof e$a$a)) {
                    v0 = v1;
                }

                if (v0 == null) {
                    return;
                }

                ((e$a$a) v0).b.setTag(v1);
                ((e$a$a) v0).b.setImageDrawable(((Drawable) v1));
                ((e$a$a) v0).h = ((com.ss.android.article.base.feature.subscribe.model.d) v1);
            }
        }
    }

    PullToRefreshListView a;
    ListView b;
    e$a c;
    SubscribeManager d;
    View.OnClickListener e;
    private Context f;
    private View g;
    private View h;
    private boolean i;
    private List j;
    private boolean k;
    private AppData l;
    private View m;
    private TextView n;
    private TextView o;
    private TextView p;
    private ImageView q;
    private ViewGroup r;
    private TextView s;
    private boolean t;
    private Map u;

    public e() {

        this.i = false;
        this.j = new ArrayList();
        this.t = false;
        this.u = new HashMap();
    }

    public final void onSubscribeDataChanged(SubscribeData arg5) {
        if (this.isViewValid()) {
            if (arg5.a == 1) {
                if (arg5.pgcSubscribeResult == 0) {
                    this.j.clear();
                    this.d.a(this.j);
                    this.c.a(this.j);
                    this.j.clear();
                    this.c.notifyDataSetChanged();
                    if (arg5.d == 1) {
                        this.b.setSelection(0);
                    }
                }

                if (!this.d.c.d && (this.a.c())) {
                    this.a.onRefreshComplete();
                }

                this.b();
            } else {
                if (arg5.a != 5) {
                    return;
                }

                if (!this.d.c.d && (this.a.c())) {
                    this.a.onRefreshComplete();
                }

                this.b();
            }
        }
    }

    final void a(String arg3) {
        FragmentActivity v0 = this.getActivity();
        if (v0 != null) {
            MobClickCombiner.onEvent(((Context) v0), "subscription", arg3);
        }
    }

    final boolean a() {
        boolean v0_1;
        FragmentActivity v0 = this.getActivity();
        if (v0 == null || !NetworkUtils.isNetworkAvailable(((Context) v0))) {
            v0_1 = false;
        } else {
            if (!this.d.c.d) {
                this.a("pull_refresh");
                this.d.b();
                this.b();
            }

            v0_1 = true;
        }

        return v0_1;
    }

    public final void afterFeedShowOnResumed() {
    }

    private void b() {
        FragmentActivity v0 = this.getActivity();
        if ((v0 instanceof IFeedManager)) {
            ((IFeedManager) v0).onLoadingStatusChanged(((IRecentFragment) this));
        }
    }

    private boolean c() {
        FragmentActivity v0 = this.getActivity();
        boolean v0_1 = (v0 instanceof IFeedManager) ? ((IFeedManager) v0).isPrimaryPage(((IRecentFragment) this)) : true;
        return v0_1;
    }

    private void d() {
        if (this.d != null) {
            com.ss.android.article.base.feature.subscribe.b.e v0 = this.d.c;
            if (v0.l) {
                v0.l = false;
            }

            if (this.d.c.d) {
                return;
            }

            if (!this.d.c.k) {
                return;
            }

            if (!NetworkUtils.isNetworkAvailable(this.getActivity())) {
                return;
            }

            this.d.b();
            this.d.c.b();
        }
    }

    public final String getCategory() {
        return "subscription";
    }

    public final int getFirstVisiblePosition() {
        return 0;
    }

    public final void handleRefreshClick(int arg2) {
        FragmentActivity v0 = this.getActivity();
        if (v0 != null && (NetworkUtils.isNetworkAvailable(((Context) v0))) && !this.d.c.d) {
            if (arg2 == 1) {
                this.a("tab_refresh");
            } else if (arg2 != 2) {
                this.a("refresh");
            }

            this.d.b();
            this.b();
        }
    }

    public final boolean isLoading() {
        boolean v0 = this.d == null || !this.d.c.d ? false : true;
        return v0;
    }

    public final void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        this.f = this.getActivity();
        SubscriptionRouter.addSubscriber(this);
        this.b.addHeaderView(this.h, null, true);
        this.c = new e$a(this.f);
        this.b.setAdapter(this.c);
        this.registerLifeCycleMonitor(this.c);
        this.d = SubscribeManager.getInstance();
        this.d.addSubscribeDataChangeListener(this);
        this.e = new View.OnClickListener() {

            public final void onClick(View arg4) {
                e.this.a("add_more");
                FragmentActivity fragmentActivity = e.this.getActivity();
                if (fragmentActivity != null) {
                    fragmentActivity.startActivity(new Intent(fragmentActivity, EntryGroupListActivity.class));
                }
            }
        };
        this.r.setOnClickListener(this.e);
        this.a.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            public final void onPullDownToRefresh(PullToRefreshBase toRefreshBase) {
                if (!e.this.a()) {
                    e.this.a.onRefreshComplete();
                }
            }
        });
        this.b.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public final void onItemClick(AdapterView arg11, View arg12, int arg13, long arg14) {
                Object v0_2;
                FragmentActivity v8 = e.this.getActivity();
                if (arg13 < e.this.b.getHeaderViewsCount()) {
                    e.this.e.onClick(arg12);
                } else {
                    int v0 = arg13 - e.this.b.getHeaderViewsCount();
                    if (v0 < 0) {
                        v0 = 0;
                    }

                    if (v8 == null) {
                        return;
                    }

                    if (e.this.c == null) {
                        return;
                    }

                    if (v0 >= e.this.c.getCount()) {
                        return;
                    }

                    com.ss.android.article.base.feature.subscribe.model.d v9 = e.this.c.a(v0);
                    if (v9 == null) {
                        return;
                    }

                    v0 = (v9.f) || v9.badge > 0 ? 1 : 0;
                    e v1 = e.this;
                    String v3 = v0 != 0 ? "click_pgc_tip" : "click_pgc";
                    long v4 = v9.b;
                    FragmentActivity v1_1 = v1.getActivity();
                    if (v1_1 != null) {
                        MobClickCombiner.onEvent(((Context) v1_1), "subscription", v3, v4, 0);
                    }

                    v9.badge = 0;
                    v9.f = false;
                    SubscribeManager v0_1 = e.this.d;
                    SubscribeManager.checkUIThread();
                    if (v9 != null) {
                        DBHelper v1_2 = DBHelper.getInstance(v0_1.mContext);
                        if (v9 != null) {
                            v9.badge = 0;
                            v9.f = false;
                            v1_2.b(108, v9);
                        }

                        com.ss.android.article.base.feature.subscribe.b.e v2 = v0_1.c;
                        Iterator v3_1 = v2.i.iterator();
                        do {
                            if (v3_1.hasNext()) {
                                v0_2 = v3_1.next();
                                if (!((d) v0_2).f && ((d) v0_2).badge <= 0) {
                                    continue;
                                }
                                v0 = 1;
                            } else {
                                v0 = 0;
                            }
                            break;
                        }
                        while (true);

                        if (v0 != 0) {
                        } else {
                            v2.b();
                        }
                    }

                    if (arg12 != null) {
                        v0_2 = arg12.getTag();
                        if (!(v0_2 instanceof com.ss.android.article.base.feature.subscribe.activity.e.e$a.e$a$a)) {
                            v0_2 = null;
                        }

                        if (v0_2 == null) {
                        } else {
                            com.ss.android.article.base.feature.subscribe.activity.e.e$a.a(((com.ss.android.article.base.feature.subscribe.activity.e.e$a.e$a$a) v0_2));
                        }
                    }

                    PgcActivity.startActivity(((Context) v8), v9.a.mId, "channel_subscription_subscribed");
                    MobClickCombiner.onEvent(v8, "pgc_profile", "click___pgc__", v9.a.mId, 0);
                }
            }
        });
        this.d.a(this.j);
        if (!this.j.isEmpty()) {
            this.c.a(this.j);
            this.c.notifyDataSetChanged();
        } else if ((this.c()) && !this.d.c.e) {
            this.d.b();
        }

        this.j.clear();
        if ((this.f instanceof IFeedManager)) {
            ((IFeedManager) this.f).addIRecentFragment(((IRecentFragment) this));
        }

        if (this.d != null) {
            this.a();
        }
    }

    public final View onCreateView(LayoutInflater arg8, ViewGroup arg9, Bundle arg10) {
        int v4 = R.string.add_pgc_subscribe;
        int v3 = R.string.add_more_pgc_subscribe;
        View v5 = arg8.inflate(com.ss.android.article.browser.R.layout.d3, arg9, false);
        this.m = arg8.inflate(com.ss.android.article.browser.R.layout.io, arg9, false);
        this.g = v5;
        this.a = v5.findViewById(com.ss.android.article.browser.R.id.listview);
        this.a.setScrollingWhileRefreshingEnabled(true);
        this.b = this.a.getRefreshableView();
        this.b.setEmptyView(this.m);
        this.h = arg8.inflate(com.ss.android.article.browser.R.layout.subscribe_header, this.b, false);
        this.l = AppData.getInstance();
        this.k = false;
        boolean v0 = android.support.design.a.m() == 2 ? true : false;
        this.t = v0;
        this.n = this.m.findViewById(com.ss.android.article.browser.R.id.subscribe_btn);
        this.o = this.m.findViewById(com.ss.android.article.browser.R.id.abg);
        this.p = this.m.findViewById(com.ss.android.article.browser.R.id.abh);
        this.q = this.m.findViewById(com.ss.android.article.browser.R.id.abf);
        this.r = this.m.findViewById(com.ss.android.article.browser.R.id.abi);
        this.s = this.h.findViewById(com.ss.android.article.browser.R.id.subscribe_header);
        if (this.l.D()) {
            TextView v1 = this.s;
            int v0_1 = this.t ? R.string.add_more_pgc_follow : v3;
            v1.setText(v0_1);
            v1 = this.n;
            v0_1 = this.t ? R.string.add_pgc_follow : v4;
            v1.setText(v0_1);
        } else {
            this.s.setText(v3);
            this.n.setText(v4);
        }

        return v5;
    }

    public final void onDestroy() {
        SubscribeManager.getInstance().b(((SubscribeDataChangeListener) this));
        super.onDestroy();
        SubscriptionRouter.removeSubscriber(this);
    }

    @Subscriber
    public final void onEnforceLoginSuccess(com.ss.android.article.base.feature.subscribe.a.a arg5) {
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

            public final void run() {
                if (e.this.getActivity() != null && e.this.a != null) {
                    e.this.a.setRefreshing(true);
                }
            }
        }, 2000);
    }

    public final void onResume() {
        int v6 = R.color.vq;
        int v5 = R.drawable.bg_subscribe_fragment_list_header;
        super.onResume();
        if (this.isViewValid()) {
            boolean v1 = AppData.isNightMode();
            if (this.k != v1) {
                this.k = v1;
                Resources v2 = this.getResources();
                this.s.setTextColor(v2.getColor(com.ss.android.article.browser.R.color.subscribe_head_text_color));
                this.n.setTextColor(v2.getColor(com.ss.android.article.browser.R.color.vu));
                UIUtils.setBackgroundResource(this.h.findViewById(com.ss.android.article.browser.R.id.subscribe_layout), v5);
                UIUtils.setBackgroundResource(this.r, v5);
                this.o.setTextColor(v2.getColor(v6));
                this.p.setTextColor(v2.getColor(v6));
                this.q.setImageResource(com.ss.android.article.browser.R.drawable.su);
                this.g.setBackgroundResource(com.ss.android.article.browser.R.color.activity_bg_color);
                this.m.setBackgroundResource(com.ss.android.article.browser.R.color.ssxinmian4);
                this.a.getLoadingLayoutProxy().setTextColor(v2.getColor(com.ss.android.article.browser.R.color.ssxinzi3));
                this.a.getLoadingLayoutProxy().setLoadingDrawable(v2.getDrawable(com.ss.android.article.browser.R.drawable.o0));
                this.a.getLoadingLayoutProxy().setProgressDrawable(v2.getDrawable(com.ss.android.article.browser.R.drawable.hq));
                this.a.getLoadingLayoutProxy().setTheme(v1);
                if (this.c != null) {
                    this.c.notifyDataSetChanged();
                }
            }
        }

        if (this.i) {
            this.i = false;
            if (this.c()) {
                this.d();
            }
        }

        CallbackCenter.notifyCallback(BaseAppData.callbackTypeEnablePostStateChanged, new Object[]{Integer.valueOf(0), "", Long.valueOf(0), ""});
    }

    public final void onSetAsPrimaryPage(int arg7) {
        if (arg7 == 1 && !this.getUserVisibleHint()) {
            this.u.clear();
            this.setUserVisibleHint(true);
        }

        if (!this.isViewValid()) {
            this.i = true;
        } else {
            this.i = false;
            this.d();
            CallbackCenter.notifyCallback(BaseAppData.callbackTypeEnablePostStateChanged, new Object[]{Integer.valueOf(0), "", Long.valueOf(0), ""});
        }
    }

    public final void onUnsetAsPrimaryPage(int arg2) {
        if (arg2 == 1 && (this.getUserVisibleHint())) {
            this.setUserVisibleHint(false);
        }
    }

    public final void setBackRefreshSwitch(boolean arg1) {
    }

    public final int supportRefreshButton() {
        return 1;
    }
}

